// pages/day_day_listen/details/details.js
var requests = require("../../../utils/requests.js");
var interval;
var totalmusic = 100
var audio
var id

var isfirst = true;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    history: true,
    buyed: false,
    collected: false,
    animationData: {},
    trans: true,
    time: 0,
    checkeditem: 0,
    seconds: 0,
    during: '00:00',
    pause: false,
    total: " -- ",
    totalmusic: 100,
    info: {}
  },
  pay: function() {
    wx.navigateTo({
      url: '../../my/pay/pay_sure',
    })
  },
  // 选集点击⌚事件
  itemclick: function(event) {
    //设置选中，并播放当前节目
    this.setData({
      checkeditem: event.currentTarget.dataset.index,
      pause: true
    })
    //关闭之前的计时器，时间、秒值清零、重新计时
    clearInterval(interval);
    this.cleartime();
    this.startaccount();
  },

  //点击播放按钮切换图标
  play: function() {
    var vm = this
    vm.setData({
      pause: !vm.data.pause
    })
    //如果是暂停，关闭记时器 、停止音频
    if (!vm.data.pause) {
      audio.pause()
      return
    } else {
      //如果是开始播放 ，开始计时，并播放音乐
      // audio.src = vm.data.info.playUrl;
    audio.play()
    }
  },

  startaccount: function() {
    var vm = this
    interval = setInterval(function() {
      var during = getApp().s_to_hs(vm.data.seconds)
      // 如果播放完成，停止计时
      if (vm.data.seconds >= vm.data.totalmusic) {
        clearInterval(interval);
        //清理掉记时器
        interval=undefined;
        vm.cleartime();
        vm.setData({
          pause: false
        })
        return
      }
      vm.setData({
        during: during,
        seconds: vm.data.seconds + 1,
      })

    }, 1000)
  },

  // 进度条拖动联动
  slidechange: function(event) {
    var vm = this
    console.log(event.detail.value);
    var during = getApp().s_to_hs(event.detail.value)
    //音频seek 到指定秒值
    audio.seek(event.detail.value)
    vm.setData({
      during: during,
      seconds: event.detail.value,
    })
  },

  // 清空事件和秒值
  cleartime: function() {
    this.setData({
      during: "00:00",
      seconds: 0,
    })
  },

  // 列表控件点击事件，滑动选择到节目选集
  listclick: function() {
    wx.pageScrollTo({
      scrollTop: 600,
      duration: 200
    })
    this.setData({
      history: false,
      buyed: true,
    })
  },

  //下一集
  next: function() {
    this.cleartime();
    var vm = this
    if (vm.data.checkeditem >= vm.data.list.length - 1) {
      return
    }
    this.setData({
      checkeditem: vm.data.checkeditem + 1
    })
  },

  //上一集
  last: function() {
    this.cleartime();
    var vm = this
    if (vm.data.checkeditem == 0) {
      return
    }
    this.setData({
      checkeditem: vm.data.checkeditem - 1
    })
  },

  //节目介绍
  history: function() {
    this.setData({
      history: true,
      buyed: false,
      collection: false,
    })
  },

  // 收藏
  tocollect: function() {
    var vm = this
    this.setData({
      collected: !vm.data.collected
    })
  },

  // 节目选集
  buyed: function() {
    this.setData({
      history: false,
      buyed: true,
      collection: false,
    })
  },


  //分享列表
  tosharelist: function() {
    wx.navigateTo({
      url: '../sharelist/sharelist',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var vm = this
    audio = wx.getBackgroundAudioManager();
    console.log(options.id)
    id = options.id;
    vm.getdata();
  },



  //  获取数据
  getdata: function() {
    var vm = this
    var data = {
      "startPoint": 0,
      "maxPage": 2
    }
    getApp().requestLoading(getApp().globalData.https + "programResource/programDetail/" + id,
      "get", data, "加载中...",
      function(res) {
        if (res.code == 0) {
          console.log(res.data)
          vm.setData({
            info: res.data
          })

          audio.src = vm.data.info.playUrl;
          audio.onCanplay(() => {
            console.log(audio.duration)
          })
          audio.onPlay(() => {
            console.log('开始播放')
            //只要音乐一开始播放，就开始计时,  
            //因为第一次进来获取时长 时会自动播放，需要手动停止掉音频,所以第一次开始播放不进行计时 
            if (isfirst) {
              audio.pause();
              isfirst = false;
            } else {
              console.log("查看是否开始计时---" + typeof interval)
              if (interval==undefined) {
                console.log("开始计时")
                vm.startaccount();
                vm.setData({
                  pause: true
                })
              }

            }
          })
          audio.onPause(() => {
            console.log('音乐被暂停')
            //只要音乐一暂停，就停止计时,
            vm.setData({
              pause: false
            })
            clearInterval(interval)
            interval=undefined;``
          })


          //开定时器  直到获取到总时长关闭记时器 
          var gettotalinterval = setInterval(function() {
            if (audio.duration != 0) {
              console.log("音频长度" + audio.duration)
              vm.setData({
                totalmusic: Math.round(audio.duration),
                total: getApp().s_to_hs(Math.round(audio.duration))
              })
              clearInterval(gettotalinterval);
            }
          }, 1000)
        }
      },
      function(res) {
        console.log(res)
      });

    //所有集数
    getApp().requestLoading(getApp().globalData.https + "/programResource/seasons/" + id,
      "get", data, "加载中...",
      function(res) {
        if (res.code == 0) {
          console.log(res.data)
          vm.setData({
            list: res.data.result
          })
        }
      },
      function(res) {
        console.log(res)
      });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    // '立即分享'  缩放动画
    this.sharenowanim()
  },

  sharenowanim: function() {
    var vm = this
    var animation = wx.createAnimation({
      duration: 0,
      timingFunction: 'linear',
    })

    this.animation = animation
    var time = 0;
    var inter = setInterval(function() {
      time = time + 200;
      if (time >= 2000) {
        clearInterval(inter)
      }
      if (vm.data.trans) {
        animation.scale(1.1, 1.1).step()
        vm.setData({
          animationData: animation.export(),
          trans: false
        })
      } else {
        animation.scale(1, 1).step()
        vm.setData({
          animationData: animation.export(),
          trans: true
        })
      }
    }, 200)
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    console.log("监听页面卸载")
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

})